import store from './../../store';
import iapi from '@/libs/iapi';

const mkeyKey = 'online_key';


//
let fetch = (cb, post) => {
	if (!cb) cb = () => {};

	let gData = getApp().globalData;

	if (!post) post = {};

	let fnFail = (code, msg) => {
		let online = gData.online = {};
		store.dispatch('online/setOnline', {
			online
		});
		cb(online);
	};

	//根本没有登录的状态
	if (!post.onlinekey && getOnlineKey() == '') return fnFail();


	iapi.vapi('online/detail', post, {
		succ: js => {
			let online = gData.online = js.online;
			store.dispatch('online/setOnline', {
				online
			});
			cb(online);
		},
		fail: fnFail,
	});
}


let getOnlineKey = () => {
	let gData = getApp().globalData;
	if (gData.onlineKey) return gData.onlineKey;

	let onlineKey = '';
	try {
		onlineKey = uni.getStorageSync(mkeyKey);
	} catch (e) {
		onlineKey = '';
	}

	//onlineKey = '6c64e2edc48a2cf5340a1b230e46bdde';

	//
	if (onlineKey) gData.onlineKey = onlineKey;

	return onlineKey;
};

let setOnlineKey = (key) => {
	if (!key) key = '';

	let gData = getApp().globalData;
	gData.onlineKey = key;

	uni.setStorage({
		key: mkeyKey,
		data: key
	});
};


let get = () => {
	let online = store.getters['online/getOnline'];
	if (online) return online;

	return {};
};

module.exports = {
	get,
	fetch,
	getOnlineKey,
	setOnlineKey,
}
